<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/
jimport( 'joomla.application.component.view' );


class JAJobBoardViewJAPermission extends JAView
{
    /**
     * Display the view
     */
    function display($tmpl = null)
    {
		$task = JRequest::getVar("task", '');
		switch ($task){
			case 'edit':				
				JAJobBoardViewJAPermission::displayForm();
				break;
			case 'bygroup':				
				JAJobBoardViewJAPermission::displayFormGroup();
				break;	
			default:
				JAJobBoardViewJAPermission::displayListItems();
		}
		parent::display($tmpl);	
  	}
  	
  	
  	/**
  	* Display List of items
  	*/
  	function displayListItems(){  				
		global $mainframe, $option;
		
		$usergroup = JRequest::getVar('usergroup', 'admin');
		
		if($usergroup=='guest') {
			JToolBarHelper::save('save_group');				
			$this->setLayout('form');
			return $this->displayForm();
		}
		
		$model = &$this->getModel();
		
		
        $option_1 = $option.'.japermission';
		$search	= $mainframe->getUserStateFromRequest( "$option_1.search", 'search', 	'',	'string' );
		$lists['search']	= JString::strtolower($search );
		
		$lists['filter_order'] = JRequest::getVar('filter_order', 'u.id');
		$lists['filter_order_Dir']	= $mainframe->getUserStateFromRequest( $option_1.'.filter_order_Dir',	'filter_order_Dir',	'',				'word' );
		$lists['option']	= $option;
		$filter_state		= $mainframe->getUserStateFromRequest( $option_1.'.filter_state', 'filter_state', '',	'word' );	
		$lists['state']		= JHTML::_('grid.state',  $filter_state );
		
		$usertype = '';
		$usertype			= $mainframe->getUserStateFromRequest( $option_1.$usergroup.'.usertype', 'usertype', '',	'string' );			
		
		$groups = JobBoardConstant::getUsergroup();
		if( $group = $groups[$usergroup] ){
			$groupHTML[] = JHTML::_('select.option', '', JText::_('Select Group...'));  
			foreach ($group as $value=>$text){
				$groupHTML[] = JHTML::_('select.option', $value, $text);  
			}			
			
			$lists['group'] = JHTML::_('select.genericlist',   $groupHTML, 'usertype', 'class="inputbox" size="1" onchange="form.submit()"', 'value', 'text', $usertype);
		}
		
        // get data items
        $items = $model->getItems($group, $usergroup, $usertype);   
		
        $pagination = &$this->get('Pagination');       
        $this->assignRef('lists', $lists);
        $this->assignRef('items', $items);
        $this->assignRef('usergroup', $usergroup);
        $this->assignRef('pagination', $pagination);
									
  	}
  	/**
  	* Display edit form
  	*/
  	function displayForm(){
  		global $option, $jbconfig;
  		
  		$current_user = JFactory::getUser();
		$usergroup = JRequest::getVar('usergroup', 'admin');
		$cid = JRequest::getVar('cid',  array(0), '', 'array');		
		$user_id = (int)$cid[0];
		$edit_user = JFactory::getUser($user_id);
		$user_per = 0;
		switch ($current_user->usertype)
		{
			case 'Manager':
				$current_user->value_type = 0;
				break;
			case 'Administrator':
				$current_user->value_type = 1;
				break;
			case 'Super Administrator':
				$current_user->value_type = 2;
				break;
			default:
				$current_user->value_type = -1;
		}
		
		switch ($edit_user->usertype)
		{
			case 'Manager':
				$edit_user->value_type = 0;
				break;
			case 'Administrator':
				$edit_user->value_type = 1;
				break;
			case 'Super Administrator':
				$edit_user->value_type = 2;
				break;
			default:
				$edit_user->value_type = -1;
		}
		
		if ($current_user->value_type<=$edit_user->value_type)
			$edit = false;
		else 
			$edit = true;
		$type = JRequest::getVar('type', '');
		
		/*If group=guest, get permission from config table*/
		$user_per = getPermissionByUserID($user_id);
		if(($user_per==-1)&&(getUserType($user_id)=='Guest')&&($usergroup!='admin')){
			$user_per = isset($jbconfig['group_permission'])?$jbconfig['group_permission']->get('Guest_permission'):0;
		}
			
		$permission_list = parsePermission($user_per, $usergroup);		
		$this->assignRef('edit', $edit);		
		$this->assignRef('rows', $permission_list);
		$this->assignRef('user_per', $user_per);
		$this->assignRef('usergroup', $usergroup);
		$this->assignRef('option', $option);
		$this->assignRef('cid', $user_id);		
		$this->assignRef('user', $current_user);
	}
	
	/**
  	* Display edit form
  	*/
  	function displayFormGroup(){  	    
  		global $option, $jbconfig;
  		
		$usergroup = JRequest::getVar('usergroup', 'admin');
		$usertype = JRequest::getVar('usertype', 'Publish Backend');
		$cid = JRequest::getVar('cid',  array(0), '', 'array');		
		$group_per = 0;		
		
		$group = str_replace(' ', '_', $usertype.'_permission');
		
		$group_per = $jbconfig['group_permission']->get($group);		
		
		$permission_list = parsePermission($group_per, $usergroup);		
			
		$groups = JobBoardConstant::getUsergroup();
		$lists = array();
		if( $group = $groups[$usergroup] ){
			foreach ($group as $value=>$text){
				$groupHTML[] = JHTML::_('select.option', $value, $text);  
			}			
			
			$lists['group'] = JHTML::_('select.genericlist',   $groupHTML, 'usertype', 'class="inputbox" size="1" onchange="form.submit()"', 'value', 'text', $usertype);
		}
			
		$this->assignRef('lists', $lists);
		$this->assignRef('rows', $permission_list);
		$this->assignRef('group_per', $group_per);
		$this->assignRef('usergroup', $usergroup);
		$this->assignRef('option', $option);
		$this->assignRef('cid', $user_id);	
	}
	
	function menu_User()
	{
		global $option;
		$usergroup=JRequest::getVar('usergroup','admin');
		
		?>
			<div class="t">
				<div class="t">
					<div class="t"></div>
				</div>
			</div>
			<div class="m">
				<ul id="submenu">
					<li>
						<a href="index.php?option=<?php echo $option; ?>&view=japermission&usergroup=admin" class="<?php if($usergroup==null||$usergroup=='admin')echo 'active' ?>">
							<?php echo JText::_( 'Admin' ); ?>
						</a>
					</li>				
					<li>			
							<a href="index.php?option=<?php echo $option; ?>&view=japermission&usergroup=user" class="<?php if($usergroup=='user')echo 'active' ?>" >
								<?php echo JText::_( 'User' ); ?>
							</a>
					</li>	
					<li>			
							<a href="index.php?option=<?php echo $option; ?>&view=japermission&usergroup=guest" class="<?php if($usergroup=='guest')echo 'active' ?>" >
								<?php echo JText::_( 'Guest' ); ?>
							</a>
					</li>					
				</ul>
				<div class="clr"></div>
			</div>
			<div class="b">
				<div class="b">
					<div class="b"></div>
				</div>
			</div>		
		<?php		
	}		
	function menu_Group()
	{
		global $option;
		$usergroup=JRequest::getVar('usergroup','admin');
		
		?>
			<div class="t">
				<div class="t">
					<div class="t"></div>
				</div>
			</div>
			<div class="m">
				<ul id="submenu">
					<li>
						<a href="index.php?option=com_jajobboard&amp;view=japermission&amp;task=bygroup&amp;usergroup=admin&usertype=Publish Back-end" class="<?php if($usergroup==null||$usergroup=='admin')echo 'active' ?>">
							<?php echo JText::_( 'Publish Back-end' ); ?>
						</a>
					</li>				
					<li>			
							<a href="index.php?option=com_jajobboard&amp;view=japermission&amp;task=bygroup&amp;usergroup=user&amp;usertype=Guest" class="<?php if($usergroup=='user')echo 'active' ?>" >
								<?php echo JText::_( 'Publish Front-end' ); ?>
							</a>
					</li>	
				</ul>
				<div class="clr"></div>
			</div>
			<div class="b">
				<div class="b">
					<div class="b"></div>
				</div>
			</div>		
		<?php		
	}	
}
?>